package com.ruoyi.web.controller.cpsp;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.cpsp.domain.RegulationType;
import com.ruoyi.cpsp.service.RegulationTypeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author neu.xiaojiacheng
 * @version 1.0
 * @date 2022/2/27 14:36
 * 规则种类Controller
 */

@CrossOrigin
@RestController
@RequestMapping("/cpsp/regulationtype")
public class RegulationTypeController {

    @Autowired
    private RegulationTypeService regulationTypeService;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/addForm", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addForm(@RequestParam String name,
                          @RequestParam String checkSide,
                          @RequestParam Boolean deleteFlag){
        RegulationType record = new RegulationType();
        record.setName(name);
        record.setCheckSide(checkSide);
        if(deleteFlag==null){
            record.setDeleteFlag(false);
            //false为在使用
        }else{
            record.setDeleteFlag(deleteFlag);
        }
        if (regulationTypeService.submitAllInfo(record))
            return AjaxResult.success();
        else
            return AjaxResult.error("保存失败");
    }

    /*返回给前端需要的规则种类*/
    //@PreAuthorize(hasPermi = "regulationtype:select")
    @ApiOperation("查找")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult select(@RequestParam Integer pageSize,
                         @RequestParam Integer currentPage,
                         @RequestParam(required = false) String name) {
        if (name == null)
            return AjaxResult.success(regulationTypeService.get(pageSize, currentPage));
        else
            return AjaxResult.success(regulationTypeService.getByName(pageSize, currentPage, name));
    }

    //@PreAuthorize("@ss.hasPermi('system:detail:edit')")
    @ApiOperation("删除")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return AjaxResult.success(regulationTypeService.delete(id));
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestParam Integer id,
                         @RequestParam String name,
                         @RequestParam String checkSide,
                         @RequestParam Boolean deleteFlag) {

        RegulationType record = new RegulationType();
        record.setId(id);
        record.setName(name);
        record.setCheckSide(checkSide);
        record.setDeleteFlag(deleteFlag);
        return AjaxResult.success(regulationTypeService.update(record));
    }


    @ApiOperation("联合Regulation_type以及Regulation_detail表查询")
    @PostMapping(value = "/selectTypeAndDetail")
    @ResponseBody
    public AjaxResult selectTypeAndDetail(RegulationType regulationType) {
        RegulationType r =regulationTypeService.selectAll().get(0);
        System.out.println(r.getName()+r.getCheckSide()+regulationTypeService.selectAll().size());
        return AjaxResult.success(regulationTypeService.getTypeAndDetail(regulationType));
    }

    @ApiOperation("查找所有type")
    @PostMapping(value = "/selectALL")
    @ResponseBody
    public AjaxResult selectALL(RegulationType regulationType) {
        return AjaxResult.success(regulationTypeService.selectAll());
    }
}
